<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属仓库" prop="depotId">
        <el-select
          v-model="queryParams.depotId"
          placeholder="请选择仓库"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="depot in depotOptions"
            :key="depot.id"
            :label="depot.name"
            :value="depot.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入记录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择操作类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录时间">
        <el-date-picker
          :editable="false"
          v-model="daterangeApplyTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['equipment:inOutRecord:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="inOutRecordList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="记录名称" align="center" prop="name" />
      <el-table-column label="所属仓库" align="center" prop="depotName" />
      <el-table-column
        label="操作类型"
        align="center"
        prop="type"
        :formatter="typeFormat"
      />
      <el-table-column
        label="记录时间"
        align="center"
        prop="applyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:stockOutDeal:deal']"
            >出库</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进出库记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="90px"
        style="border: solid 1px rgb(21 144 206 / 20%)"
      >
        <el-divider content-position="center">申请信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入编号"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入记录名称"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进出库类型">
              <el-radio-group v-model="form.inOut" :disabled="true">
                <el-radio
                  v-for="dict in inOutOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择操作类型"
                :disabled="true"
              >
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人" prop="applyName">
              <el-input
                v-model="form.applyName"
                placeholder="请输入记录名称"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间" prop="applyTime">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.applyTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择记录时间"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="所属仓库" prop="depotId">
                <el-select
                  v-model="form.depotId"
                  placeholder="请选择仓库"
                  :disabled="openDisabled"
                >
                  <el-option
                    v-for="depot in depotOptions"
                    :key="depot.id"
                    :label="depot.name"
                    :value="depot.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请状态" prop="status">
                <el-select v-model="form.status" :disabled="true">
                  <el-option
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">主管审核</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主管名称" prop="checkDirector">
              <el-input
                v-model="form.checkDirectorName"
                placeholder="请输入主管检查"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="checkDirectorTime">
              <el-input
                v-model="form.checkDirectorTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请输入主管检查时间"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核备注" prop="checkDirectorRemark">
              <el-input
                v-model="form.checkDirectorRemark"
                type="textarea"
                placeholder="请输入主管检查备注"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">工厂负责人审核</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核人名" prop="checkPlantLeader">
              <el-input v-model="form.checkPlantLeaderName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="checkPlantLeaderTime">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.checkPlantLeaderTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核备注" prop="checkPlantLeadeRemark">
              <el-input
                v-model="form.checkPlantLeadeRemark"
                type="textarea"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.type == '04'">
          <el-divider content-position="center">生产部审核</el-divider>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审核人名" prop="checkProduction">
                <el-input v-model="form.checkProductionName" :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="审核时间" prop="checkProductionTime">
                <el-date-picker
                  :editable="false"
                  clearable
                  size="small"
                  v-model="form.checkProductionTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :disabled="true"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="审核备注" prop="checkProductionRemark">
                <el-input
                  v-model="form.checkProductionRemark"
                  type="textarea"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请状态" prop="status">
                <el-select
                  v-model="form.status"
                  placeholder="请选择养护级别"
                  :disabled="true"
                >
                  <el-option
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-row>

        <el-divider content-position="center">进出库记录-物资列信息</el-divider>
        <!--        <el-row :gutter="10" class="mb8">-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEqpDepotInOutGoods" :disabled="openDisabled">添加</el-button>-->
        <!--          </el-col>-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEqpDepotInOutGoods" :disabled="openDisabled">删除</el-button>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-table
          :data="eqpDepotInOutGoodsList"
          :row-class-name="rowEqpDepotInOutGoodsIndex"
          @selection-change="handleEqpDepotInOutGoodsSelectionChange"
          ref="eqpDepotInOutGoods"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            min-width="200"
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column min-width="200" label="编号" prop="code">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.code"
                placeholder="请输入编号"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column min-width="200" label="物资名称" prop="goodsId">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.goodsName"
                placeholder="请输入物资名称"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column min-width="200" label="规格" prop="goodsId">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.model"
                placeholder="请输入物资名称"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column min-width="200" label="申请数量" prop="num">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.num"
                placeholder="请输入申请数量"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column min-width="200" label="单位" prop="unit">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.unit"
                placeholder="请输入单位"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column
            min-width="200"
            label="现有库存量"
            prop="inventoryNum"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.inventoryNum"
                placeholder="请输入现有库存量"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column min-width="200" label="可用库存量" prop="usableNum">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.usableNum"
                placeholder="请输入现有库存量"
                :disabled="true"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm" :loading="loadingch"
          >出库完成</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInOutRecord,
  getInOutRecord,
  delInOutRecord,
  addInOutRecord,
  updateInOutRecord,
  outDeal,
} from "@/api/equipment/materials/stockOutDeal";
import { listDepot } from "@/api/equipment/materials/depot";
export default {
  name: "InOutRecord",
  data() {
    return {
      loadingch: false,
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEqpDepotInOutGoods: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进出库记录表格数据
      inOutRecordList: [],
      // 进出库记录-物资列表格数据
      eqpDepotInOutGoodsList: [],
      // 所属仓库
      depotOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 进出库类型字典
      inOutOptions: [],
      // 操作类型字典
      typeOptions: [],
      // 记录时间时间范围
      daterangeApplyTime: [],
      // 是否有效字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        code: null,
        name: null,
        inOut: null,
        type: null,
        applyTime: null,
        checkDirector: null,
        checkDirectorTime: null,
        checkDirectorRemark: null,
        checkPlantLeader: null,
        checkPlantLeaderTime: null,
        checkPlantLeadeRemark: null,
        checkProduction: null,
        checkProductionTime: null,
        checkProductionRemark: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "记录名称不能为空", trigger: "blur" },
        ],
        inOut: [
          { required: true, message: "进出库类型不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "操作类型不能为空", trigger: "change" },
        ],
        applyTime: [
          { required: true, message: "记录时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("in_out_stock").then((response) => {
      this.inOutOptions = response.data;
    });
    this.getDicts("out_stock_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("out_stock_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDepot();
  },
  methods: {
    getDepot() {
      this.loading = true;
      let depotParams = {
        type: "01",
        parentId: null,
        id: null,
      };
      listDepot(depotParams).then((response) => {
        this.depotOptions = response.data;
        this.loading = false;
      });
    },
    /** 查询进出库记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeApplyTime && "" != this.daterangeApplyTime) {
        this.queryParams.params["beginApplyTime"] = this.daterangeApplyTime[0];
        this.queryParams.params["endApplyTime"] = this.daterangeApplyTime[1];
      }
      listInOutRecord(this.queryParams).then((response) => {
        this.inOutRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 进出库类型字典翻译
    inOutFormat(row, column) {
      return this.selectDictLabel(this.inOutOptions, row.inOut);
    },
    // 操作类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 是否有效字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        inOut: "0",
        type: null,
        applyTime: null,
        checkDirector: null,
        checkDirectorName: null,
        checkDirectorTime: null,
        checkDirectorRemark: null,
        checkPlantLeader: null,
        checkPlantLeaderName: null,
        checkPlantLeaderTime: null,
        checkPlantLeadeRemark: null,
        checkProduction: null,
        checkProductionName: null,
        checkProductionTime: null,
        checkProductionRemark: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
      };
      this.eqpDepotInOutGoodsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeApplyTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加进出库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getInOutRecord(id).then((response) => {
        this.form = response.data;
        this.eqpDepotInOutGoodsList = response.data.eqpDepotInOutGoodsList;
        this.open = true;
        this.title = "出库";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getInOutRecord(id).then((response) => {
        this.form = response.data;
        this.eqpDepotInOutGoodsList = response.data.eqpDepotInOutGoodsList;
        this.open = true;
        this.title = "查看记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.eqpDepotInOutGoodsList = this.eqpDepotInOutGoodsList;
        if (this.form.id != null) {
          if (
            this.form.eqpDepotInOutGoodsList != "undefined" &&
            this.form.eqpDepotInOutGoodsList != null
          ) {
            let m = true;
            for (let i = 0; i < this.form.eqpDepotInOutGoodsList.length; i++) {
              if (
                this.form.eqpDepotInOutGoodsList[i].inventoryNum <
                this.form.eqpDepotInOutGoodsList[i].num
              ) {
                m = false;
                this.$message.error(
                  this.form.eqpDepotInOutGoodsList[i].goodsName +
                    "的现有库存量低于申请数量！"
                );
                break;
              }
              // if(this.form.eqpDepotInOutGoodsList[i].usableNum<0){
              //     m=false;
              //     this.$message.error(this.form.eqpDepotInOutGoodsList[i].goodsName+"的可用库存量已经小于0！");
              //     break;
              // }
            }
            if (m) {
              this.loadingch = true;
              outDeal(this.form)
                .then((response) => {
                  this.msgSuccess("出库成功");
                  this.open = false;
                  this.getList();
                  this.loadingch = false;
                })
                .catch((error) => {
                  this.loadingch = false;
                });
            }
          }
        } else {
          this.loadingch = true;
          const rLoading = this.openLoading();
          addInOutRecord(this.form)
            .then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              rLoading.close();
              this.loadingch = false;
              this.getList();
            })
            .catch((error) => {
              this.loadingch = false;
              rLoading.close();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除进出库记录编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInOutRecord(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 进出库记录-物资列序号 */
    rowEqpDepotInOutGoodsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 进出库记录-物资列添加按钮操作 */
    handleAddEqpDepotInOutGoods() {
      let obj = {};
      obj.code = "";
      obj.goodsId = "";
      obj.num = "";
      obj.unit = "";
      obj.inventoryNum = "";
      obj.status = "";
      obj.remark = "";
      this.eqpDepotInOutGoodsList.push(obj);
    },
    /** 进出库记录-物资列删除按钮操作 */
    handleDeleteEqpDepotInOutGoods() {
      if (this.checkedEqpDepotInOutGoods.length == 0) {
        this.$alert("请先选择要删除的进出库记录-物资列数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.eqpDepotInOutGoodsList.splice(
          this.checkedEqpDepotInOutGoods[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleEqpDepotInOutGoodsSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.eqpDepotInOutGoods.clearSelection();
        this.$refs.eqpDepotInOutGoods.toggleRowSelection(selection.pop());
      } else {
        this.checkedEqpDepotInOutGoods = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/inOutRecord/export",
        {
          ...this.queryParams,
        },
        "进出库记录" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
